/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;

/**
 * <p>Project: sboot2021 - Index
 * <p>Powered by webrx On 2021-07-29 16:04:39
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
@Controller
public class Index {
    @GetMapping("/")
    public String index() {
        return "index";
    }

    @PostMapping("/upfile")
    public String upfile(@RequestParam("file") MultipartFile file, Model m) {
        String msg = "上传成功";
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        String fileName = file.getOriginalFilename();
        String filePath = "d:/userfiles";
        System.out.println(filePath);
        File dest = new File(filePath, fileName);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        m.addAttribute("msg", msg);
        return "upfile";
    }

    @PostMapping("/upfiles")
    public ModelAndView upfiles() {
        var mv = new ModelAndView("upfiles");
        String msg = "上传成功";

        mv.addObject("msg", msg);
        return mv;
    }
}
